package org.clockworkmages.games.anno1186.generator.sets;

import org.clockworkmages.games.anno1186.TimeConstants;
import org.clockworkmages.games.anno1186.dao.GameObjectsList;
import org.clockworkmages.games.anno1186.generator.GameObjectGenerator;
import org.clockworkmages.games.anno1186.model.Effecting;
import org.clockworkmages.games.anno1186.model.FetishConstants;
import org.clockworkmages.games.anno1186.model.character.SkillConstants;
import org.clockworkmages.games.anno1186.model.character.StatConstants;
import org.clockworkmages.games.anno1186.model.effect.Effect;
import org.clockworkmages.games.anno1186.model.option.GenericOption;
import org.clockworkmages.games.anno1186.model.situation.GenericSituation;
import org.clockworkmages.games.anno1186.scripting.tools.ScriptBuilder;
import org.clockworkmages.games.anno1186.scripting.tools.TextBuilder;

public class PalosCardPlayer
		extends GameObjectGenerator
		//
		implements StatConstants, SkillConstants, TechnicalSituationConstants,
		TimeConstants {

	public static final String STATE = "STATE";
	public static final String STATE_NEW = "NEW";

	public String getFileName() {
		return "palos_cardplayer";
	}

	@Override
	public GameObjectsList generate() {
		GameObjectsList gameObjects = new GameObjectsList();

		GenericOption option = new GenericOption();
		GenericSituation situation = new GenericSituation();

		String STATE_NEGOTIATING = "NEGOTIATING";
		String STATE_WON = "WON";
		String STATE_LOST = "LOST";
		String STATE_LOST_SUBMISSION = "LOST_SUBMISSION";

		String PLOT_PALOS_TAVERN_CARDPLAYER_ISGONE = "PALOS_TAVERN_CARDPLAYER_ISGONE";

		// located options
		option = new GenericOption();
		option.setSituationId("PALOS_TAVERN");
		option.setAddToStack(true);
		option.setSee("two locals playing cards");
		option.setLabel("Card players");
		option.setIcon("talk");
		option.setGoToSituation("PALOS_TAVERN_CARDPLAYER");
		option.setCondition("!"
				+ ScriptBuilder.NEW().util().isDay().and().not().plot()
						.is(PLOT_PALOS_TAVERN_CARDPLAYER_ISGONE).toString());
		gameObjects.getOptions().add(option);

		// situations:
		situation = new GenericSituation();
		situation.setId("PALOS_TAVERN_CARDPLAYER");
		switchState(situation, STATE_NEW);
		situation.setTitle("Diego, the gambler sailor");
		situation.setText(//
				"As you approach the table where the two men were playing cards, one of them gets up, throws his cards onto the table angrily and walks out the tavern's door, cursing under his nose.\n"
						+ "The other player happily gathers the cards and the coins lying on the table and then looks up at you, flashing you a beaming smile smile.\n"
						+ //
						"He is a young man, maybe twenty, and not unhandsome. With his relaxed pose, tanned skin and cheap, comfortable clothes he could probably be a sailor on a shore leave.\n"
						+ TextBuilder
								.NEW()
								.iff()
								.plot()
								.get("BIRTH")
								.equalsString("SCUM")
								.then(//
								"{f:i}[Origin]{f} You actually know that guy - it's Diego, a sailor and a gambler that you've seen in Palos several times before. He something cheats in cards, but generally he's good company.\n")
								.asText()
						+ " - Well hello there, friend! - he says as you come closer - ${if "
						+ ScriptBuilder.NEW().plot().get("BIRTH")
						+ "=='SCUM'}Name's Diego. ${fi}Care for a game of cards? I just won a few coins from that poor sod and it's only fair that I lose them to you.\n");
		gameObjects.getSituations().add(situation);

		// STATE=NEW
		// win the game
		option = new GenericOption();
		option.setCondition(checkState(STATE_NEW).and().me().getGold().ge(50)
				.and().util().random(100).le(40).toString());
		switchState(option, STATE_WON);
		option.getEffects().add(
				new Effect(ScriptBuilder.NEW().me().addGold(50).toString()));
		option.getEffects().add(
				new Effect(ScriptBuilder.NEW().plot()
						.set(PLOT_PALOS_TAVERN_CARDPLAYER_ISGONE).toString()));
		option.setLabel("Sure, let's play!");
		option.setTextAfter(" - Sure, let's play!\n"
				+ "Diego deals the cards and you actually get a strong hand with three aces. You get two more cards and get one more ace! You easily beat Dieago's two pairs of eights and dames. You win!\n"
				+ "- Damn, it, you're one lucky "
				+ TextBuilder.NEW().iff().me().isMale().then("bastard")
						.elsee("wench").asText()
				+ " - says Diego - Here's your gold. I'm broke, again, so I'm done gambling for tonight.");
		situation.getOptions().add(option);

		// lose the game
		option = new GenericOption();
		option.setOr(true); // condition
		option.setCondition(checkState(STATE_NEW).and().me().getGold().ge(50)
				.toString());
		// switchState(option, STATE_NEW); // target
		option.getEffects().add(
				new Effect(ScriptBuilder.NEW().me().addGold(-50).toString())); // effect
		option.setLabel("Sure, let's play!");
		option.setTextAfter("- Well... ok. Let's play!\n"
				+ "Diego deals the cards and you get an average hand - you only have a pair of tens. You exchange three cards but only get a five. Diego easily beats you with his three dames. You lose!!\n"
				+ "- Well, lucky me - says Diego with a smile as he collects the coins from the table. - How about we play another round, for 50 coins again?\n");
		situation.getOptions().add(option);

		option = new GenericOption();
		option.setOr(true);
		option.setCondition(checkState(STATE_NEW)
				.and()
				.util()
				.fetishesEnabled(FetishConstants.MEN,
						FetishConstants.SUBMISSION).toString());
		switchState(option, STATE_NEGOTIATING);
		option.setLabel("I don't have that sort of money");
		option.setTextAfter("- I don't have that much money.\n"
				+ "Diego looks at you for a moment, rubbing his chin thoughtfully.\n"
				+ " - I see - he says eventually - Fortunately for you, I have a weak spot for comely ${if "
				+ ScriptBuilder.NEW().me().isMale()
				+ "}lads${else}lasses${fi} like you. How about we play anyway. You win, you take my 50 coins. I win, you spend the next hour in my room with your lips . One way or another, it's your lucky night.");
		situation.getOptions().add(option);

		// NEGOTIATING
		option = new GenericOption();
		option.setCondition(checkState(STATE_NEGOTIATING).and().util()
				.random(100).le(40).toString());
		switchState(option, STATE_WON);
		option.getEffects().add(
				new Effect(ScriptBuilder.NEW().me().addGold(50).toString()));
		option.setLabel("Well... ok. Let's play!");
		option.setTextAfter("- Well... ok. Let's play!\n"
				+ "Diego deals the cards and you actually get a strong hand with three aces. You get two more cards and get one more ace! You easily beat Dieago's two pairs of eights and dames. You win!\n"
				+ "- Damn, it, you're one lucky "
				+ "${if "
				+ ScriptBuilder.NEW().me().isMale()
				+ "}bastard${else}wench${fi}"
				+ " - says Diego - Here's your gold. I'm broke, again, so I'm done gambling for tonight.");
		situation.getOptions().add(option);

		option = new GenericOption();
		option.setOr(true);
		option.setCondition(checkState(STATE_NEGOTIATING).toString());
		switchState(option, STATE_LOST_SUBMISSION);
		option.setLabel("Well... ok. Let's play!");
		option.setTextAfter("- Well... ok. Let's play!\n"
				+ "Diego deals the cards and you get an average hand - you only have a pair of tens. You exchange three cards but only get a five. Diego easily beats you with his three dames. You lose!!\n"
				+ "- Well, lucky me - says Diego with a smile - I guess it's time to pay up, so let's go upstairs to my room so you can start working.\n");
		situation.getOptions().add(option);

		// LOST_SUBMISSION
		option = new GenericOption();
		option.setCondition(checkState(STATE_LOST_SUBMISSION).toString());
		option.setGoToSituation("PALOS_TAVERN_CARDPLAYER_SUBMIT");
		option.setTimePassed(1 * HOUR);
		option.setLabel("Damn it... ok, let's get it over with. (Submit)");
		situation.getOptions().add(option);

		option = new GenericOption();
		option.setCondition(checkState(STATE_LOST_SUBMISSION)
				.and()
				.util()
				.roll(//
				ScriptBuilder.NEW().me().getEffectiveStat(PERCEPTION)
						.toString()
						+ "+20"
				//
				, "PALOS_SPOT_MARKED_CARDS").toString());
		option.setGoToSituation(ST_EMPTY_RETURN);
		option.getEffects().add(
				new Effect(ScriptBuilder.NEW().plot()
						.set(PLOT_PALOS_TAVERN_CARDPLAYER_ISGONE).toString()));
		option.setLabel("Hey, those cards are marked! (Perception)");
		option.setTextAfter("Looking at the cards you notice that some of them were marked with tiny scratches on their backs. The scratches are not easy to notice, but they're definitely there. \n"
				+ " - Hey, those cards are marked!- you exclaim as you realize you were conned, drawing the attention of some of the other patrons of the taverns. \n"
				+ " - Shhh... - whispers Diego, looking around nervously - Ok, ok, maybe they are. No need to make such a fuss about it, noone here wants trouble. Just... forget the whole thing, you owe me nothing. Now if you'll excuse me, I need to be going.\n"
				+ "That said, Diego leaves the tavern in a hurry, followed by some hostile looks of people who overheard you talking..");
		situation.getOptions().add(option);

		//
		option = new GenericOption();
		checkStates(STATE_WON, STATE_LOST, STATE_NEGOTIATING);
		option.setGoToSituation(ST_EMPTY_RETURN);
		option.setIcon("back");
		option.setLabel("Need to be going, c'ya!");
		option.setTextAfter(" - Need to be going, c'ya!\n - Sure, take care!");
		situation.getOptions().add(option);

		// PALOS_TAVERN_CARDPLAYER_SUBMIT
		situation = new GenericSituation();
		situation.setId("PALOS_TAVERN_CARDPLAYER_SUBMIT");
		situation.setText(//
				"You follow Diego upstairs to his room [TODO]\n");
		gameObjects.getSituations().add(situation);
		situation.getEffects().add(
				new Effect(ScriptBuilder.NEW().me()
						.increaseStat(REPUTATION, -2).toString()));
		situation.getEffects().add(
				new Effect(ScriptBuilder.NEW().plot()
						.set(PLOT_PALOS_TAVERN_CARDPLAYER_ISGONE).toString()));
		gameObjects.getSituations().add(situation);

		option = new GenericOption();
		option.setGoBack(true);
		option.setIcon("back");
		option.setLabel("Go back downstairs");
		situation.getOptions().add(option);

		return gameObjects;
	}

	protected void switchState(Effecting effecting, String stateValue) {
		Effect effect = new Effect(ScriptBuilder.NEW().situation()
				.setState(STATE, stateValue).toString());
		effecting.getEffects().add(effect);
	}

	protected ScriptBuilder checkStates(String... states) {
		ScriptBuilder scriptBuilder = ScriptBuilder.NEW();
		for (String state : states) {
			scriptBuilder = scriptBuilder.situation().getState(STATE)
					.equalsString(state);
		}
		return scriptBuilder;
	}

	protected ScriptBuilder checkState(String stateValue) {
		return ScriptBuilder.NEW().situation().getState(STATE)
				.equalsString(stateValue);
	}

}
